package com.etc.hotel.controller;

import com.etc.hotel.bean.User;
import com.etc.hotel.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/doLogin", method = RequestMethod.POST)
    public Map<String, Object> doLogin(@RequestBody Map<String, Object> map, HttpSession session) {
        Map<String, Object> resultMap = new HashMap<>();

        User user = userService.doLogin(map.get("account").toString());
        System.out.println(user);
        if (user == null) {
            resultMap.put("code", 201);
            resultMap.put("msg", "账号不存在");
        } else if (!user.getPassword().equals(map.get("password").toString())) {
            resultMap.put("code", 202);
            resultMap.put("msg", "密码错误");
        } else {
            resultMap.put("code", 200);
            resultMap.put("msg", "登录成功");
            resultMap.put("user", user);
            session.setAttribute("user", user);
        }
        return resultMap;
    }

}
